/**
 * Created by wzq on 15-4-17.
 */

define([
    'jquery',
    'underscore',
    'backbone',
    'ViewClassLoader',
], function ($, _, Backbone, ViewClassLoader) {

    var blueInkApp = {};
    blueInkApp.history = []; // url: xxx, reload:false

    // dom ready
    $(function () {
        $(".tit > ul:gt(0)").hide();
        $(".tit").click(function () {
            $(this).children("ul").slideDown(300).end()
                .siblings().children("ul").slideUp(300);
        });
    });

    // 获取视图容器
    blueInkApp.rootContainer = null;
    blueInkApp.__getContainer = function () {
        if (!blueInkApp.rootContainer) {
            blueInkApp.rootContainer = $('#blueInkContainer');
        }

        return blueInkApp.rootContainer;
    };

    var maskStat = false;
    blueInkApp.setMask = function (maskShow) {
        if (maskStat === maskShow) {// 状态不变
            return;
        }

        if (maskShow) {
            console.log('show mask');
            $('#mask_bg').css("display", "block");
        } else {
            console.log('hide mask');
            $('#mask_bg').css("display", "none");
        }
        maskStat = maskShow;
    };

    blueInkApp.appendNewView = function (el) {
        blueInkApp.__getContainer().append(el);
    };

    // 路由
    var Router = blueInkApp.Router = Backbone.Router.extend({
        routes: {
            '*path(?*qs)': 'any'
        },
        any: function (path, qs) {
            console.log('route to => ' + path + "?" + qs);
            if (!path) {
                return;
            }

            var stackSize = blueInkApp.history.length;
            if (stackSize >= 2 && blueInkApp.history[stackSize - 2].path === path) {
                blueInkApp.goBack(qs);// goback
            } else {
                blueInkApp.addNewView(path, qs);
            }
        }
    });


    blueInkApp.addNewView = function (path, qs) {
        var needShowMask = true;
        setTimeout(function () {
            if (needShowMask) {
                blueInkApp.setMask(true);
            }
        }, 100); // in 100ms, not show mask

        var stackSize = blueInkApp.history.length;
        var currView = stackSize >= 1 ? blueInkApp.history[stackSize - 1].view : null;
        ViewClassLoader.getViewClass(path, function (ViewClass) {
            var nextView = new ViewClass();

            nextView.onRender(qs, function (view) {
                blueInkApp.history.push({
                    path: path,
                    view: view
                });

                if (currView) {
                    currView.hide();
                }

                blueInkApp.appendNewView(view.el);
                blueInkApp.animateIn(view);// animate
                needShowMask = false;
                blueInkApp.setMask(false);

                //setTimeout(function () {
                //    blueInkApp.appendNewView(view.el);
                //    blueInkApp.animateIn(view);// animate
                //    blueInkApp.setMask(false);
                //}, 2000);

            });
        });
    };

    blueInkApp.animateIn = function (view, done) {
        view.$el.addClass('animated slideInRight');
        view.$el.one('webkitAnimationEnd', function () {
            console.log('animate done');
            view.$el.removeClass('animated slideInRight');
            if (done) {
                done(view);
            }
        })
    };

    blueInkApp.animateOut = function (view, done) {
        view.$el.addClass('animated slideOutRight');
        view.$el.one('webkitAnimationEnd', function () {
            console.log('animate done');
            view.$el.removeClass('animated slideOutRight');
            if (done) {
                done(view);
            }
        })
    };

    blueInkApp.goBack = function (qs) {
        console.log('goBack');
        var stackSize = blueInkApp.history.length;
        var nextView = blueInkApp.history[stackSize - 2].view;
        var currView = blueInkApp.history.pop().view;

        blueInkApp.animateOut(currView, function (view) {
            currView.hide();
            currView.remove();
            nextView.onReshow(qs, function (view) {
                // default do nothing
            });
        });
    };

    // 启动
    var router = new Router();
    Backbone.history.start();
    //Backbone.history.start({pushState: false, silent: false});

    // add to window
    this.blueInkApp = blueInkApp;
    return blueInkApp;
});